package cn.exrick.xboot.modules.graphics.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResponseUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.graphics.entity.GraphicsPage;
import cn.exrick.xboot.modules.graphics.param.GraphicsPageParam;
import cn.exrick.xboot.modules.graphics.service.IGraphicsPageService;
import cn.exrick.xboot.pojo.MethodRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author njp
 */
@Slf4j
@RestController
@Api(description = "首页画布表管理接口")
@RequestMapping("/xboot/graphicsPage")
@Transactional
public class GraphicsPageController {

    @Autowired
    private IGraphicsPageService iGraphicsPageService;
    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<GraphicsPage> get(@PathVariable String id){
        GraphicsPage graphicsPage = iGraphicsPageService.getById(id);
        return new ResultUtil<GraphicsPage>().setData(graphicsPage);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<GraphicsPage>> getAll(){

        List<GraphicsPage> list = iGraphicsPageService.list();
        return new ResultUtil<List<GraphicsPage>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<GraphicsPage>> getByPage(@ModelAttribute PageVo page){

        IPage<GraphicsPage> data = iGraphicsPageService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<GraphicsPage>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<GraphicsPage> saveOrUpdate(@ModelAttribute GraphicsPage graphicsPage){

        if(iGraphicsPageService.saveOrUpdate(graphicsPage)){
            return new ResultUtil<GraphicsPage>().setData(graphicsPage);
        }
        return new ResultUtil<GraphicsPage>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){
        GraphicsPageParam graphicsPageParam = new GraphicsPageParam();
        for(String id : ids){
            graphicsPageParam.setId(id);
            iGraphicsPageService.delete(graphicsPageParam);
            //iGraphicsPageService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value = "通过组织id获取界面列表")
    public Result<IPage<GraphicsPage>> page(GraphicsPageParam graphicsPageParam,PageVo page){
        IPage<GraphicsPage> page1 = iGraphicsPageService.page(graphicsPageParam, PageUtil.initMpPage(page), false);
        return new ResultUtil<IPage<GraphicsPage>>().setData(page1);
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增界面")
    public Result<Object> add( @Validated(GraphicsPageParam.add.class) GraphicsPageParam graphicsPageParam){
        if(iGraphicsPageService.add(graphicsPageParam).getFlag()) {
            return new ResultUtil<Object>().setSuccessMsg("添加成功");
        }
        return new ResultUtil<Object>().setErrorMsg("添加失败");
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "查看（根据id获取界面数据）")
    public Result<GraphicsPage> detail(@Validated(GraphicsPageParam.detail.class) GraphicsPageParam graphicsPageParam){
        GraphicsPage graphicsPage = iGraphicsPageService.detail(graphicsPageParam);
        return new ResultUtil<GraphicsPage>().setData(graphicsPage);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑界面信息")
    public Result<Object> edit(@Validated(GraphicsPageParam.detail.class) GraphicsPageParam graphicsPageParam){
        if(iGraphicsPageService.edit(graphicsPageParam)) {
            return new ResultUtil<Object>().setSuccessMsg("保存成功");
        }
        return new ResultUtil<Object>().setErrorMsg("保存失败");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除界面")
    public Result<Object> delete(@Validated(GraphicsPageParam.delete.class) GraphicsPageParam graphicsPageParam){
        if(iGraphicsPageService.delete(graphicsPageParam)) {
            return new ResultUtil<Object>().setSuccessMsg("删除成功");
        }
        return new ResultUtil<Object>().setErrorMsg("删除失败");
    }

    /**
     *
     * @param graphicsPageParam 保存界面设计接口
     * @return
     */
    @RequestMapping(value = "/saveLayout", method = RequestMethod.POST)
    @ApiOperation(value = "界面设计的保存接口")
    public Result<Object> saveLayout(@RequestBody @Validated(GraphicsPageParam.saveLayout.class) GraphicsPageParam graphicsPageParam){
        iGraphicsPageService.saveLayout(graphicsPageParam);
        return new ResultUtil<Object>().setSuccessMsg("保存成功");
    }

    @RequestMapping(value = "/start", method = RequestMethod.POST)
    @ApiOperation(value = "启用/应用界面")
    public Result<Object> start( @Validated(GraphicsPageParam.start.class) GraphicsPageParam graphicsPageParam){
        iGraphicsPageService.startGraphicsPage(graphicsPageParam);
        return new ResultUtil<Object>().setSuccessMsg("启用成功");
    }
    @RequestMapping(value = "/copyLayout", method = RequestMethod.POST)
    @ApiOperation(value = "复制界面")
    public Result<Object> copyLayout( @Validated(GraphicsPageParam.copy.class) GraphicsPageParam graphicsPageParam){
        iGraphicsPageService.copyGraphicsPage(graphicsPageParam);
        return new ResultUtil<Object>().setSuccessMsg("复制成功");
    }

    @RequestMapping(value = "/getUserLayoutOrgStart", method = RequestMethod.GET)
    @ApiOperation(value = "获取（用户）组织下启动状态的界面数据")
    public Result<GraphicsPage> getUserLayoutOrgStart(){
        GraphicsPageParam graphicsPageParam = new GraphicsPageParam();
        User currUser = securityUtil.getCurrUser();
        graphicsPageParam.setOrgId(currUser.getDepartmentId());
        GraphicsPage gPbyOrgAndStatus = iGraphicsPageService.getGPbyOrgAndStatus(graphicsPageParam);
        return new ResultUtil<GraphicsPage>().setData(gPbyOrgAndStatus);
    }



}
